# Maintainer: Peter Budai <peterbudai@hotmail.com>

_realname=scikit-learn
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.3.0
pkgrel=2
pkgdesc="A set of python modules for machine learning and data mining (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://scikit-learn.org'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-python-scipy"
         "${MINGW_PACKAGE_PREFIX}-python-joblib"
         "${MINGW_PACKAGE_PREFIX}-python-threadpoolctl")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-wheel"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        001-mingw-python-openmp-flag.patch
        002-openmp-cleanup-wait.patch
        003-Fix-optimization-flag.patch
        004-unpin-numpy-version.patch)
sha256sums=('8be549886f5eda46436b6e555b0e4873b4f10aa21c07df45c4bc1735afbccd7a'
            '8ec903dec6dbe788e24d3d7e800f933d6380329d6427c1873947aac7d5a74023'
            '88c35335818321275e5d8b1a851d967faff48ad628c0dd5954e955689cc5d6b9'
            '653f294ec5185ff2df98144e67327ed85ba5412e29e8e5b936074894025ba540'
            'd663fa43506a22172714826c55d8014176762ee9b4fe325b2f257e835c106fe1')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    001-mingw-python-openmp-flag.patch \
    002-openmp-cleanup-wait.patch \
    003-Fix-optimization-flag.patch \
    004-unpin-numpy-version.patch

  cd ${srcdir}
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE

  install -d ${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}
  cp -r doc/tutorial ${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/tutorial
}
